<?php
/**
 * Created by PhpStorm.
 * User: yys
 * Date: 2016/11/19
 * Time: 17:53
 */

namespace App\Services\BrandService;

use App\Services\UserService\UserTool;
use App\Services\Tool\GeneralTool;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class BrandToolService
{

    /**
     * 品牌搜索
     * @param Request $request
     */
    public function getSearchBrandList(Request $request)
    {
        $search = trim($request->input('search',''));
        $search=empty($search)?'':$search;
        $brands = DB::table('brands')->where('state',1)
            ->where('brand_name','like','%'.$search.'%')
            ->orderBy('sort','desc')
            ->get();
        return $brands;
    }

    /***
     * 获取所有品牌
     * @return mixed
     */
    public static function getAllBrands()
    {
        $brands = DB::table('brands')->where('state',1)
            ->get()
              ->toArray();
        return $brands;
    }
    /***
     * 同步品牌商信息到品牌表中
     * @param array $data
     * @return bool
     */
    public static function syncBrandUser(array $data){
        try{
            $user_id=$data['user_id'];
            //需要判断用户是不是品牌商
            $brand['user_id']=$user_id;
            if(isset($data['avatar'])){
                $brand['logo']=$data['avatar'];
            }
            if(isset($data['brand_name'])){
                $brand['brand_name']=$data['brand_name'];
            }
            $save_res=self::saveBrandsDB($brand);
            return $save_res;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }

    /***
     * 保存品牌
     * @param array $data
     * @return bool
     */
    private static function saveBrandsDB(array $data){
        try{
            $user_id=$data['user_id'];
            $brand_user_exist=self::brandUserExist($user_id);
            $data['updated_at']=time();
            $data['state']=1;
            DB::beginTransaction();
            if($brand_user_exist){
                $res=DB::table('brands')->where(array('user_id'=>$user_id))
                    ->update($data);
            }else{
                $data['created_at']=time();
                $data['brand_id']=GeneralTool::only_no();
                $res=DB::table('brands')->insert($data);
            }
            return ($res==1)?true:false;
        }catch (\Exception $e){
            DB::rollback();
            Log::info($e);
            return false;
        }finally{
            DB::commit();
        }
    }

    /***
     * 判断品牌商是否已经创建品牌用户
     * @param $user_id
     * @return bool
     */
    public static function brandUserExist($user_id){
        if(empty($user_id)){
            return false;
        }
        $num=DB::table('brands')->where(array('user_id'=>$user_id))
            ->count();
        return ($num==0)?false:true;
    }

    /***
     * 判断品牌是不是存在
     * @param $id  自增Id
     * @return bool
     */
    public static function brandExistById($id){
        if(empty($id)){
            return false;
        }
        $num=DB::table('brands')->where(array('id'=>$id,'state'=>1))
            ->count();
        return ($num==0)?false:true;
    }

    /***
     * 判断品牌是不是存在
     * @param $brand_id 品牌Id
     * @return bool
     */
    public static function brandExistByBrandId($brand_id){
        if(empty($brand_id)){
            return false;
        }
        $num=DB::table('brands')->where(array('brand_id'=>$brand_id,'state'=>1))
            ->count();
        return ($num==0)?false:true;
    }

    /***
     * 获取品牌
     * @param $brand_id 品牌Id
     * @return null
     */
    public static function getBrandByBrandId($brand_id){
        if(empty($brand_id)){
            return null;
        }
        $brand=DB::table('brands')->where(array('brand_id'=>$brand_id,'state'=>1))
            ->first();
        return $brand;
    }
    public static function getBrandNameByBrandId($brand_id){
        if(empty($brand_id)){
            return '';
        }
        $brand_name=DB::table('brands')->where(array('brand_id'=>$brand_id,'state'=>1))
            ->value('brand_name');
        return empty($brand_name)?'':$brand_name;
    }
    /***
     * 获取用户类型
     * @param $user_id
     * @return mixed
     */
    public static function getUserType($user_id)
    {
        $user_tool=new UserTool();
        $user_type=$user_tool->userType($user_id);
        return $user_type;
    }

    /***
     * 获取品牌数组
     * @param array $brand_ids
     * @return mixed
     */
    public static function getBrandsWhereNotInBrandIds(array $brand_ids){
        $brands=DB::table('brands')
            ->where(array('state'=>1))
            ->whereNotIn('brand_id',$brand_ids)
            ->select('brand_id','brand_name')
            ->get()
            ->toArray();
        return $brands;
    }
}